package com.gipson.amc.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dsbj.common.msg.DataPageMessage;
import com.dsbj.common.msg.ResponseMessage;
import com.gipson.amc.controller.BaseController;
import com.gipson.amc.entity.Application;
import com.gipson.amc.entity.Authority;
import com.gipson.amc.entity.PageByDataTable;
import com.gipson.amc.model.TreeNode;
import com.gipson.amc.service.basicdata.ApplicationService;
import com.gipson.amc.service.basicdata.AuthorityService;
import com.gipson.amc.utils.Constants;
import com.gipson.amc.utils.strprocess.CompareUtil;
import com.gipson.amc.utils.strprocess.ObjectParse;
import com.gipson.amc.utils.strprocess.StrUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 权限控制器类。
 *
 * @author cjw 2020-05-06
 */
@Controller
@RequestMapping(value = "/authority")
public class AuthorityController extends BaseController {
	
	public static String viewPrefix = "/basicdata/authority";
	
	@Resource
	private AuthorityService authorityService;
	
	@Resource
	private ApplicationService applicationService;
	
	
	/**
	 * @param
	 * @return
	 * @author cjw 2020-03-23
	 */
	@RequiresPermissions("authority.list")
	@RequestMapping(value = "/list")
	public String list(ModelMap modelMap) {
		//获取应用列表
		List<Application> applicationList = applicationService.getApplicationList();
		modelMap.put("applicationList", applicationList);
		//获取默认application
		String applicationCode = "AMC";
		Application applicationDefault = applicationService.getApplicationByCode(applicationCode);
		modelMap.put("applicationDefault", applicationDefault);
		return viewPrefix + "/list";
	}
	
	/**
	 * datatable展示》获取"树状结构"的权限列表。
	 * 传入参数: nameDim,applicationId
	 *
	 * @param
	 * @return
	 * @author cjw 2020-03-24
	 */
	@RequestMapping(value = "/getAuthorityAllByPage")
	@ResponseBody
	public DataPageMessage getAuthorityAllByPage(PageByDataTable pageByDataTable, String nameDim, String applicationId) {
		//根据nameDim,applicationId模糊查询authority列表。
		List<Authority> authorityList = authorityService.getAuthorityByNameDimByApplicationId(nameDim, applicationId);
		JSONArray jsonArray = AuthorityService.generateAuthority(authorityList);
		return DataPageMessage.ok(jsonArray, jsonArray.size(), jsonArray.size(), 0, "");
	}
	
	/**
	 * 新增、修改页面。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-03-25
	 */
	@RequestMapping(value = "/edit")
	public String edit(String applicationId, String id, ModelMap modelMap) {
		
		/**
		 * 根据id判断该页面的类型("新增页面"、"修改页面")。
		 */
		//若id为空,该页面为"新增页面"。
		if (StrUtil.isEmpty(id)) {
			//applicationId
			if (StrUtil.isEmpty(applicationId)) {
				modelMap.put("errMsg", "applicationId为null!");
				return Constants.ERROR_PAGE;
			}
			//获取application
			Application application = applicationService.getById(applicationId);
			//将application.Id,applicationCode存入authority中。
			Authority authority = new Authority();
			authority.setApplicationId(application.getId());
			authority.setApplicationCode(application.getApplicationCode());
			authority.setApplicationName(application.getApplicationName());
			modelMap.put("authority", authority);
		} else {
			//若id不为空,返回"修改页面"。
			Authority authority = authorityService.getAuthorityFullById(id);
			modelMap.put("authority", authority);
		}
		
		//获取imgUrl列表。
		List<String> imgUrlList = AuthorityService.getImgUrlList();
		modelMap.put("imgUrlList", imgUrlList);
		return viewPrefix + "/edit";
	}
	
	/**
	 * ztree展示》获取权限树。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-07
	 */
	@RequestMapping(value = "/getAuthorityTree")
	@ResponseBody
	public ResponseMessage getAuthorityTree(String applicationId) {
		//解析传来的参数
		if (StrUtil.isEmpty(applicationId)) {
			return ResponseMessage.error("applicationId为空!");
		}
		
		/**
		 * 获取权限列表,并以"权限树格式"输出。
		 */
		List<Authority> authorityList = authorityService.getAuthority(applicationId);
		List<TreeNode> treeNodeList = new ArrayList<>();
		TreeNode treeNode = null;
		
		for (Authority authority : authorityList) {
			treeNode = new TreeNode();
			treeNode.setId(authority.getCode());
			//treeNode.type
			if (CompareUtil.equal(authority.getType(), 1)) {
				treeNode.setType(1);
				treeNode.setOpen(true);
				treeNode.setIcon("/static/imgs/tree/cate.png");
			}
			if (CompareUtil.equal(authority.getType(), 2)) {
				treeNode.setType(2);
				treeNode.setIcon("/static/imgs/tree/material.png");
			}
			treeNode.setPId(authority.getParentCode());
			treeNode.setName(authority.getName());
			treeNode.setValue(authority.getCode());
			treeNodeList.add(treeNode);
		}
		return ResponseMessage.ok(treeNodeList);
	}
	
	/**
	 * 存入authority信息。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-07
	 */
	@RequestMapping(value = "/addAuthority")
	@ResponseBody
	public ResponseMessage addAuthority(Authority authority) {
		
		//解析传来的参数
		if (StrUtil.isEmpty(authority.getApplicationId())) {
			return ResponseMessage.error("applicationId为空!");
		}
		if (StrUtil.isEmpty(authority.getCode())) {
			return ResponseMessage.error("code为空!");
		}
		if (StrUtil.isEmpty(authority.getName())) {
			return ResponseMessage.error("name为空!");
		}
		if (StrUtil.isEmpty(authority.getType())) {
			return ResponseMessage.error("type为空!");
		}
		
		/**
		 * 验证传入的参数是否有效。
		 * 检查authority》检查code,name是否有效。
		 */
		ResponseMessage responseMessageTemp = authorityService.checkAuthorityForCodeAndName(null, authority.getCode(), authority.getName(), authority.getApplicationId());
		if (!responseMessageTemp.isSuccess()) {
			return responseMessageTemp;
		}
		
		/**
		 * 存入authority信息。
		 */
		authority.setDelFlag(0);
		authorityService.save(authority);
		return ResponseMessage.ok();
	}
	
	/**
	 * 更新authority信息。
	 * 传入参数id,
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-07
	 */
	@RequestMapping(value = "/updateAuthority")
	@ResponseBody
	public ResponseMessage updateAuthority(Authority authority) {
		ResponseMessage responseMessageTemp = authorityService.updateAuthority(authority);
		return responseMessageTemp;
	}
	
	/**
	 * 检查用户填写的code,name是否有效。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-07
	 */
	@RequestMapping(value = "/checkCodeAndName")
	@ResponseBody
	public ResponseMessage checkCodeAndName(String id, String code, String name, String applicationId) {
		
		//解析传来的参数
		if (StrUtil.isEmpty(code)) {
			return ResponseMessage.error("code为空!");
		}
		if (StrUtil.isEmpty(name)) {
			return ResponseMessage.error("name为空!");
		}
		if (StrUtil.isEmpty(applicationId)) {
			return ResponseMessage.error("applicationId为空!");
		}
		
		//检查用户填写的code,name是否有效。
		ResponseMessage responseMessage = authorityService.checkAuthorityForCodeAndName(id, code, name, applicationId);
		return responseMessage;
	}
	
	/**
	 * 根据id删除authority, 若该authority下含有子菜单,会被一并删除。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-08
	 */
	@RequestMapping(value = "/deleteAuthority")
	@ResponseBody
	public ResponseMessage deleteAuthority(String id) {
		//解析传来的参数
		if (StrUtil.isEmpty(id)) {
			return ResponseMessage.error("id为空!");
		}
		
		//根据id删除authority, 若该authority下含有子菜单,会被一并删除。
		ResponseMessage responseMessage_temp = authorityService.deleteAuthority(id);
		return responseMessage_temp;
	}
	
	/**
	 * 新增下级菜单页面。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-08
	 */
	@RequestMapping(value = "/addSubAuthority")
	public String addSubAuthority(String id, ModelMap modelMap) {
		//解析传来的参数
		if (StrUtil.isEmpty(id)) {
			modelMap.put("errMsg", "id为空!");
			return Constants.ERROR_PAGE;
		}
		
		//根据id获取authority信息。
		Authority authority = authorityService.getAuthorityFullById(id);
		if (authority == null) {
			modelMap.put("errMsg", "未找到该id对应的authority信息!id=" + id);
			return Constants.ERROR_PAGE;
		}
		
		//将authority.applicationId,applicationCode,code,name存入authority_2中。
		Authority authority2 = new Authority();
		authority2.setApplicationId(authority.getApplicationId());
		authority2.setApplicationCode(authority.getApplicationCode());
		authority2.setApplicationName(authority.getApplicationName());
		authority2.setParentCode(authority.getCode());
		authority2.setParentName(authority.getName());
		modelMap.put("authority", authority2);
		
		//获取imgUrl列表。
		List<String> imgUrlList = AuthorityService.getImgUrlList();
		modelMap.put("imgUrlList", imgUrlList);
		return viewPrefix + "/edit";
	}
	
	/**
	 * 图标选择页面(iconSelect页面)。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-09
	 */
	@RequestMapping(value = "/iconSelect")
	public String iconSelect(String value) {
		value = ObjectParse.parse2Str(value);
		return viewPrefix + "/iconSelect";
	}
	
	
}
